// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui'
import './assets/css/font-awesome/css/font-awesome.css'
import 'element-ui/lib/theme-chalk/index.css'
import tabsRouter from './components/tabsRouter'
// import sha256 from 'js-sha256'
import {aesEncrypt} from '@/utils/crypto'
import store from './store'
import masterComponents from './router/masterComponents' // 所有不同项目合集组件
import { getUserCookie, deleteUserCookie, getUserPermiss } from '@/utils/auth'
// 全局引用图标组件
// import FaIcon from '@/components/commons/FaIcon.vue'
import './components/global'

// 模拟mock数据
// import './mock'

Vue.use(tabsRouter, { tabMaps: masterComponents })
Vue.use(ElementUI)
// 全局注册图标组件
// Vue.component('fa-icon', FaIcon)

Vue.prototype.aes256 = aesEncrypt

Vue.config.productionTip = false

router.beforeEach((to, from, next) => {
  const name = to.name
  const { token } = getUserCookie()
  if (!token) {
    deleteUserCookie(false)
    if (name !== 'LOGIN') {
      router.push({ name: 'LOGIN' })
    }
  }
  next()
})
Vue.prototype.$_permission = function (val) {
  let { permissionCodes } = getUserPermiss() // 调用sdk获取权限
  return permissionCodes.indexOf(val) !== -1
}
// 按钮权限
Vue.directive('permission', {
  bind: function (el, binding) {
    let onlyValue = binding.value
    if (!Vue.prototype.$_permission(onlyValue)) {
      if (!el.parentNode) {
        el.style.display = 'none'
      } else {
        el.parentNode.removeChild(el)
      }
    }
  }
})
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})
